import 'package:business_1812a/bean/category_parent_opt_bean.dart';
import 'package:business_1812a/service/service_method.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CategoryPage extends StatefulWidget {
  @override
  _CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {
  List<CategoryParentBean> parentList;

  @override
  void initState() {
    // _getParentCategory();
    super.initState();
  }

  //获取一级分类数据源
  Future _getParentCategory() async {
    await request('categoryGoodsOpt', {'parent_opt_id': 0}).then((value) =>
        parentList = CategoryParentResult.fromJson(value)
            .entity
            .goods_opt_get_response
            .goods_opt_list);
    return parentList;
  }

  //一级分类的item
  Widget _leftInkWell(index) {
    return InkWell(
      onTap: () {},
      child: Container(
        height: ScreenUtil().setHeight(100),
        decoration: BoxDecoration(
            color: Colors.white,
            border:
                Border(bottom: BorderSide(width: 1, color: Colors.black12))),
        child: Text(
          parentList[index].opt_name,
          style: TextStyle(fontSize: ScreenUtil().setSp(28)),
        ),
      ),
    );
  }

  Widget _leftCategoryNav() {
    return Container(
      width: ScreenUtil().setWidth(180),
      decoration: BoxDecoration(
          border: Border(right: BorderSide(width: 1.0, color: Colors.black12))),
      child: ListView.builder(
          itemCount: parentList.length,
          itemBuilder: (context, index) {
            return _leftInkWell(index);
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, height: 1334, width: 750);
    return Container(
      child: FutureBuilder(
        future: _getParentCategory(),
        builder: (context, val) {
          if (val.hasData) {
            return _leftCategoryNav();
          } else {
            return Center(
              child: Text('加载中'),
            );
          }
        },
      ),
    );
  }
}
